package com.haiya.domain.aggregate.cargo;

import com.haiya.domain.aggregate.cargo.valueobject.DeliverySpecification;

/**
 * 货物类
 * 包含货物的id、发送方电话、描述、以及交货说明(发货城市、收货城市)
 * @author qiaoguoqiang
 */
public class Cargo {
    private String id;
    private String senderPhone;
    private String description;
    private DeliverySpecification delivery;

    public Cargo(String id) {
        this.id = id;
    }

    public Cargo() {
    }

    /**
     * 工厂方法: 预定新的货物
     * @param id
     * @param senderPhone
     * @param description
     * @param delivery
     * @return
     */
    public static Cargo newCargo(String id,
                                 String senderPhone,
                                 String description,
                                 DeliverySpecification delivery) {
        Cargo cargo = new Cargo(id);
        cargo.senderPhone = senderPhone;
        cargo.description = description;
        cargo.delivery = delivery;
        return cargo;
    }

    /**
     * 修改收货地址
     *  发货地址和收货地址不能相同，相同抛出异常
     * @param destinationLocationCode
     */
    public void changeDelivery(String destinationLocationCode) {
        if (this.delivery.getOriginLocationCode().equals(destinationLocationCode)) {
            throw new IllegalArgumentException(
                    "destination and origin location cannot be the same.");
        }
        this.delivery.setDestinationLocationCode(destinationLocationCode);
    }

    /**
     * 修改发货地址
     * @param senderPhone
     */
    public void changeSender(String senderPhone) {
        this.senderPhone = senderPhone;
    }

    /**
     * Getters
     */

    public String id() {
        return id;
    }

    public String sender() {
        return senderPhone;
    }

    public String description() {
        return description;
    }

    public DeliverySpecification delivery() {
        return delivery;
    }
}
